package com.cn.springcloudwechat.controller.personalcenter;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserPoint;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserControllerH {

    @Autowired
    YmUserService ymUserService;

    @Autowired
    YmUserPointService ymUserPointService;

    /**
     * 修改用户资料（前后台通用）
     * @param ajaxRes
     * @param ymUser
     * @return
     */
    @RequestMapping("updateUser")
    @LogInfo(operateType = OperateType.UPDATE, content = "修改用户资料（前后台通用）")
    public AjaxRes updateUser(AjaxRes ajaxRes, YmUser ymUser){
        try{
            if(ymUser.getUserId() == null){
                ymUser.setUserId(ThreadLocalUtil.getUser().getUserId());
            }
            if(ymUser.getHeadimgurl() != null){
                ymUserPointService.insertUserPoint(ymUser.getUserId(),"上传头像获得",2,"+20");
            }
            Integer result = ymUserService.updateUser(ymUser);
            ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 条件查询所有用户（后台接口）
     * @param ajaxRes
     * @param ymUser
     * @return
     */
    @RequestMapping("getUserList")
    @LogInfo(operateType = OperateType.QUERY, content = "条件查询所有用户（后台接口）")
    public AjaxRes getUserList(AjaxRes ajaxRes,YmUser ymUser,Page<YmUser> page){
       try{
           Page<YmUser> ymUserPage = ymUserService.getUserList(ymUser,page);
           ajaxRes.setSucceed(ymUserPage,Const.DATA_SUCCEED);
       }catch (Exception e){
           ajaxRes.setFailMsg(e.getMessage());
       }
       return ajaxRes;
    }

    /**
     * 修改用户状态
     * @param ajaxRes
     * @param state
     * @param userId
     * @return
     */
    @RequestMapping("updateUserState")
    @LogInfo(operateType = OperateType.UPDATE, content = "修改用户状态")
    public AjaxRes updateUserState(AjaxRes ajaxRes,@RequestParam("state") Integer state,
                                   @RequestParam("userId[]") String[] userId){
        try {
            ymUserService.updateUserState(state,userId);
            ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        } catch (Exception e) {
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }





    /**
     * 获取用户豆豆总数
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getNewestPoint")
    @LogInfo(operateType = OperateType.QUERY, content = "获取用户豆豆总数")
    public AjaxRes getNewestPoint(AjaxRes ajaxRes){
        try{
            Long userId = ThreadLocalUtil.getUser().getUserId();
            YmUserPoint ymUserPoint = ymUserPointService.getNewestPoint(userId);
            Long total = ymUserPoint.getTotalPoint();
            ajaxRes.setSucceed(total,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }





}
